﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Documents;
using Macad.Common;
using Macad.Core.Geom;
using Macad.Core.Shapes;
using Macad.Core.Topology;
using Macad.Occt;
using SpCad;
using SpCad.Tools;

namespace SPCAM.Plugin.Cam.Model
{
    public class WeldTreeModel
    {
        public Body? TBody { get; set; }
        private TopoDS_Edge topoDS_Edge;
        public Trsf? _trsf{get;set;}
        public string? Name { get; set; } 
        public TopoDS_Edge  TopoDSEdge { get=>topoDS_Edge; set {  topoDS_Edge = value;   } }
        public ShapeBase Shapebase { get => ShapeTools.Create(TopoDSEdge); }
        public AIS_Shape AIS_Shape { get => new AIS_Shape(topoDS_Edge); }
        private List<EdgePropertyModel>? _EdgePropertyModels;
        public List<EdgePropertyModel>? EdgePropertyModels { get => _AddEdgeProperties();     }
        public WeldTreeModel(TopoDS_Edge edge,Body body=null)
        {
            TopoDSEdge = edge;
            if (body == null)
            {
               
                TBody = SpServer.Instance.SelectBody;

            }
            else
            {
                TBody = body;
            } 
        }
        public WeldTreeModel(TopoDS_Edge edge, Trsf? trsf  )
        {
            TopoDSEdge = edge;

            _trsf = trsf;
        
        }

      
        #region 添加属性
        private void _AddProperty(string typename,string name,string value)
        {
            _EdgePropertyModels ??= new List<EdgePropertyModel>();
            var edpromod= new EdgePropertyModel { Name = name, Value = value.ToString() };
            _EdgePropertyModels.Add(edpromod);
        }
        List<EdgePropertyModel>? _AddEdgeProperties()
        {
            _EdgePropertyModels?.Clear();
            TopoDS_Edge edge = topoDS_Edge;
            const string edgecat = "Edge";
            const string curvecat = "Curve";
 

            var flags = "";
            if (BRep_Tool.Degenerated(edge))
                flags += "Degenerated ";
            if (BRep_Tool.SameParameter(edge))
                flags += "SameParameter ";
            if (BRep_Tool.SameRange(edge))
                flags += "SameRange ";

            //_AddProperty(edgecat, "已关闭", $"{(BRep_Tool.IsClosed(edge) ? "Yes" : "No")}"); 

            //_AddProperty(edgecat, "宽容", $"{BRep_Tool.Tolerance(edge)}");
            if (!flags.IsEmpty())
                //_AddProperty(edgecat, "标识", flags);

            if (BRep_Tool.IsGeometric(edge))
            {
                // 3D curve
                double first = 0, last = 0;
                var curve = BRep_Tool.Curve(edge, ref first, ref last);
                    if (curve != null)
                    {
                        _AddProperty(edgecat, "参数", $"({Math.Round(first, 1)}, {Math.Round(last, 2)})");
                        var _StartPoint = curve.Value(first);
                        var _EndPoint = curve.Value(last);

                        _AddProperty("", "起点", "X " + _StartPoint.X.ToRoundedString() + ",Y " + _StartPoint.Y.ToRoundedString() + ",Z " + _StartPoint.Z.ToRoundedString());
                        _AddProperty("", "终点", "X " + _EndPoint.X.ToRoundedString() + ",Y " + _EndPoint.Y.ToRoundedString() + ",Z " + _EndPoint.Z.ToRoundedString());


                       // _AddProperty(curvecat, "类型", curve.GetType().Name.Replace("Geom_", ""));
                        //if (curve.IsPeriodic())
                        //    _AddProperty(curvecat, "期限", $"{curve.Period()}");
                        //_AddProperty(curvecat, "连续", curve.Continuity().ToString().Replace("GeomAbs_", ""));

                        var props = new GProp_GProps();
                        BRepGProp.LinearProperties(edge, props);
                        _AddProperty(edgecat, "长度", $"{Math.Round(props.Mass(), 2)}");
                     
                        switch (curve)
                        {
                            case Geom_Line line:
                                Name = "直线";
                                const string linecat = "Line";
                                _AddProperty(linecat, "类型", "直线");
                                var lineLoc = line.Position().Location;
                                _AddProperty(linecat, "位置", $"({lineLoc.X.ToRoundedString()}, {lineLoc.Y.ToRoundedString()}, {lineLoc.Z.ToRoundedString()})");
                                var lineDir = line.Position().Direction;
                                _AddProperty(linecat, "方向", $"({lineDir.X.ToRoundedString()}, {lineDir.Y.ToRoundedString()}, {lineDir.Z.ToRoundedString()})");
                                break;

                            case Geom_Circle circle:
                                Name = "圆";

                                const string circlecat = "Circle";
                                _AddProperty(circlecat, "类型", "圆");
                                _AddProperty(circlecat, "半径", $"{circle.Radius().ToRoundedString()}");
                                var circleLoc = circle.Position().Location;
                                _AddProperty(circlecat, "位置", $"({circleLoc.X.ToRoundedString()}, {circleLoc.Y.ToRoundedString()}, {circleLoc.Z.ToRoundedString()})");
                                var circleDir = circle.Position().Direction;
                                _AddProperty(circlecat, "方向", $"({circleDir.X.ToRoundedString()}, {circleDir.Y.ToRoundedString()}, {circleDir.Z.ToRoundedString()})");
                                var circleXDir = circle.Position().XDirection;
                                _AddProperty(circlecat, "X-方向", $"({circleXDir.X.ToRoundedString()}, {circleXDir.Y.ToRoundedString()}, {circleXDir.Z.ToRoundedString()})");
                                var circleYDir = circle.Position().YDirection;
                                _AddProperty(circlecat, "Y-方向", $"({circleYDir.X.ToRoundedString()}, {circleYDir.Y.ToRoundedString()}, {circleYDir.Z.ToRoundedString()})");
                                break;

                            case Geom_Ellipse ellipse:
                                Name = "椭圆";

                                const string ellipsecat = "Ellipse";
                                _AddProperty(ellipsecat, "类型", "椭圆");
                                _AddProperty(ellipsecat, "短半径", $"{ellipse.MajorRadius().ToRoundedString()}");
                                _AddProperty(ellipsecat, "长半径", $"{ellipse.MinorRadius().ToRoundedString()}");
                                _AddProperty(ellipsecat, "离心率", $"{ellipse.Eccentricity().ToRoundedString()}");
                                _AddProperty(ellipsecat, "焦点", $"{ellipse.Focal().ToRoundedString()}");
                                var ellipseFocus = ellipse.Focus1();
                                _AddProperty(ellipsecat, "焦点1", $"({ellipseFocus.X.ToRoundedString()}, {ellipseFocus.Y.ToRoundedString()}, {ellipseFocus.Z.ToRoundedString()})");
                                ellipseFocus = ellipse.Focus2();
                                _AddProperty(ellipsecat, "焦点1 2", $"({ellipseFocus.X.ToRoundedString()}, {ellipseFocus.Y.ToRoundedString()}, {ellipseFocus.Z.ToRoundedString()})");
                                var ellipseLoc = ellipse.Position().Location;
                                _AddProperty(ellipsecat, "位置", $"({ellipseLoc.X.ToRoundedString()}, {ellipseLoc.Y.ToRoundedString()}, {ellipseLoc.Z.ToRoundedString()})");
                                var ellipseDir = ellipse.Position().Direction;
                                _AddProperty(ellipsecat, "方向", $"({ellipseDir.X.ToRoundedString()}, {ellipseDir.Y.ToRoundedString()}, {ellipseDir.Z.ToRoundedString()})");
                                var ellipseXDir = ellipse.Position().XDirection;
                                _AddProperty(ellipsecat, "X-方向", $"({ellipseXDir.X.ToRoundedString()}, {ellipseXDir.Y.ToRoundedString()}, {ellipseXDir.Z.ToRoundedString()})");
                                var ellipseYDir = ellipse.Position().YDirection;
                                _AddProperty(ellipsecat, "Y-方向", $"({ellipseYDir.X.ToRoundedString()}, {ellipseYDir.Y.ToRoundedString()}, {ellipseYDir.Z.ToRoundedString()})");
                                break;

                            case Geom_BezierCurve bezier:
                                const string beziercat = "贝塞尔曲线";
                                _AddProperty(beziercat, "类型", "贝塞尔曲线");
                                Name = "贝塞尔曲线";

                                _AddProperty(beziercat, "阶数", $"{bezier.Degree()}");
                                _AddProperty(beziercat, "控制点数", $"{bezier.NbPoles()}");
                                _AddProperty(beziercat, "是否有理", $"{(bezier.IsRational() ? "Yes" : "No")}");
                                break;

                            case Geom_BSplineCurve bspline:
                                const string bsplinecat = "B-Spline Curve";
                                _AddProperty(bsplinecat, "类型", "贝塞尔曲线");
                                Name = "贝塞尔曲线";

                                _AddProperty(bsplinecat, "阶数", $"{bspline.Degree()}");
                                _AddProperty(bsplinecat, "控制点数t", $"{bspline.NbPoles()}");
                                _AddProperty(bsplinecat, "节点数", $"{bspline.NbKnots()}");
                                _AddProperty(bsplinecat, "节点分布.", bspline.KnotDistribution().ToString().Replace("GeomAbs_", ""));
                                _AddProperty(bsplinecat, "是否有理", $"{(bspline.IsRational() ? "Yes" : "No")}");
                                break;
                        }
                    }
            }
            else
            {
                // Curve on surface, currently not supported
            }
            return _EdgePropertyModels;


        }
        #endregion
    }

    public class EdgePropertyModel
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }
}
